package ihuotui.ftl.html;

import java.io.IOException;
import java.io.StringWriter;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import junit.framework.Test;
import freemarker.cache.ClassTemplateLoader;
import freemarker.template.Configuration;
import freemarker.template.DefaultObjectWrapper;
import freemarker.template.Template;
import freemarker.template.TemplateException;
import ihuotui.ftl.util.FileUtil;

public class MainTest {
	public static void main(String[] args) {
		Configuration configuration = new Configuration();
		configuration.setObjectWrapper(new DefaultObjectWrapper());
		configuration.setTemplateLoader(new ClassTemplateLoader(MainTest.class, "/"));
		try {
			Template template = configuration.getTemplate("temp.ftl");
			StringWriter writer = new StringWriter();
			Map<String, Object> context = new HashMap<String, Object>();

			context.put("message", "我的第一个Maven_FreeMarker程序");

			List<Animal> animals = new ArrayList<Animal>();
			Animal a1 = new Animal();
			a1.setName("ihuotui");
			animals.add(a1);
			Animal a2 = new Animal();
			a2.setName("test");
			animals.add(a2);
			context.put("animals", animals);

			context.put("names", Utils.getNames(Animal.class));

			template.process(context, writer);

			System.out.println(writer.toString());

			String file = FileUtil.defaultEntityFile("temp", MainTest.class);
			FileUtil.writeData(writer.toString(), file);
			System.out.println(file);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (TemplateException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
}